//页面初始化
$(function(){
	// 初始化table
	initTable();
	//关闭新增窗口
	$("#closeBtn").click(function() {
		backendCommon.closeWin("noPassWin");
	});
});


// 查询接入方信息
function searchJob(){
	var jobName = $("#search_job_name").val();
	var jobGroup = $("#search_job_group").val();
	var jobStatus = $("#search_job_status").val();
	//删除ajax缓存
	var rn = Math.random();
	$('#data_result').datagrid('getPager').pagination({
		 'pageNumber' : 1
	});
	$('#data_result').datagrid('options').url=mainDomain +'/job/findScheduleJobsPgByDTO.action?rn='+rn;
	$('#data_result').datagrid('options').queryParams = {
		'jobName':jobName,
		'jobGroup':jobGroup,
		'jobStatus':jobStatus
	};
	$('#data_result').datagrid('reload');
}

//新增编辑窗口
function showConfDiv(user_type,id){
	$('#dlg_form')[0].reset();
	$('#id').val('');
	if(user_type == 'add'){
		$('#dlg').dialog('setTitle','配置新增');
		$.pro.mask('show');
		$('#dlg').dialog('open');
	}else{
		$('#dlg').dialog('setTitle','配置编辑');
		// 查询信息
		$.post(mainDomain +'/job/findScheduleJobById.action',{'id':id},function(result){
			if (result.success) {
				$('#id').val(id);
				$('#jobName').val(result.data.job.jobName);
				$('#jobGroup').val(result.data.job.jobGroup);
				$('#cronExpression').val(result.data.job.cronExpression);
				$('#createBy').val(result.data.job.updateBy);
				$('#jobClass').val(result.data.job.jobClass);
				$('#jobDesc').val(result.data.job.jobDesc);
				$.pro.mask('show');
				$('#dlg').dialog('open');
			} else {
				$.messager.alert('提示', result.message, 'error');
			}
		},'json');
	}
}

function save(){
	var params =$('#dlg_form').serializeObject();
	$.post(mainDomain +'/job/saveScheduleJob.action',params,function(result){
		if (result.success && result.state == 1) {
			$.messager.alert('提示','操作成功', 'info');
			searchJob();
			$.pro.cancelDiv("dlg");
			$.pro.mask('hide');
		} else {
			$.messager.alert('提示', result.message, 'error');
		}
	},'json');
	
}

function updateJob(id,jobStatus){
	$.messager.confirm('确认', '是否确认进行该操作?', function(r){
		if (r){
			var params = {'id':id,'jobStatus':jobStatus};
			$.post(mainDomain +'/job/updateScheduleJob.action',params,function(result){
				if (result.success && result.state == 1) {
					$.messager.alert('提示','操作成功', 'info');
					searchJob();
				} else {
					$.messager.alert('提示', result.message, 'error');
				}
			},'json');
		};
	});
}

//初始化列表页面
function initTable() {
	$('#data_result').datagrid({
		title:'定时器列表信息',
		width : backendCommon.getWidth(0.94),
		height : 'auto',
		loadMsg:"数据装载中,请稍等....",
		nowrap: false,
		rownumbers:true,
		striped : true,
		fitColumns: true,
		pagination:true,
        singleSelect:true,
        pageSize:10,
	    pageNumber:1,          
	    pageList:[10,15,20],
		columns : [[
		            {field : 'jobName',title : '定时器名称',width :$(this).width() * 0.20,align : 'center'},
					{field : 'jobGroup',title : '定时器分组',width :$(this).width() * 0.11,align : 'center'},
					{field : 'cronExpression',title : 'cron_expression',width : $(this).width() * 0.13,align : 'center'},
					{field : 'jobClass',title : '执行路径',width : $(this).width() * 0.30,align : 'center'},
					{field : 'jobStatus',title : '状态',width :$(this).width() * 0.08,align : 'center',
						formatter : function(value, rec, index) {
							if(value == 0){
								return "停用";
							}
							if(value == 1){
								return "<b><font color='red'>启用</font></b>";
							}
							if(value == 2){
								return "删除";
							}
						}	
					},
					{field : 'jobDesc',title : '说明',width :$(this).width() * 0.20,align : 'center'},
					{field : 'createTime',title : '创建时间',width :$(this).width() * 0.20,align : 'center',
						formatter : function(value, rec, index) {
							var str = "";
							if(value != '') {
								str = backendCommon.dateTimeToLocalStringWithSeconds(rec.createTime);
							}
							return str;
						}	
					},
					{field : 'uid',title : '操作',width : $(this).width() * 0.35,align : 'center',
						 formatter:function(value,rec){
							 var  b = '<a class="publish" onclick="showConfDiv(\'edit\','+rec.id+');" href="javascript:void(0)"  style="width:80px" >修改</a>&nbsp;&nbsp;';
							 if(rec.jobStatus == 0){
								 b += '<a class="publish" onclick="updateJob('+rec.id+',\'1\');" href="javascript:void(0)"  style="width:80px" >启用</a>&nbsp;&nbsp;';
							 }
							 if(rec.jobStatus == 1){
								 b += '<a class="publish" onclick="updateJob('+rec.id+',\'0\');" href="javascript:void(0)"  style="width:80px" >停用</a>&nbsp;&nbsp;';
							 }
							 b += '<a class="publish" onclick="updateJob('+rec.id+',\'3\');" href="javascript:void(0)"  style="width:80px" >立即执行一次</a>&nbsp;&nbsp;';
							 b += '<a class="publish" onclick="updateJob('+rec.id+',\'2\');" href="javascript:void(0)"  style="width:80px" >删除</a>';
							 return b;
						   }
					}
				]]
	});
	$('#data_result').datagrid('getPager').pagination({
		 'buttons' : [{
			    text:'新增配置',
	            iconCls:'icon-add',
	            handler:function(){
	            	showConfDiv('add');
	            }
	        }]
	});
};
